package com.dbdata.move;

import com.wksc.commons.utils.string.StringUtils;
import opensource.jpinyin.PinyinFormat;
import opensource.jpinyin.PinyinHelper;

/**
 * 拼音工具类
 *
 * @author ChengJianLong
 */
public class PinYinTool {
    private PinYinTool() {}

    public static String getPinYin(String str) {
        return PinyinHelper.convertToPinyinString(str, " ", PinyinFormat.WITHOUT_TONE);
    }

    public static String getPinYinSX(String str) {
        String pinYin = getPinYin(str);
        if (StringUtils.isNotBlank(pinYin)) {
            StringBuilder pinYinSx = new StringBuilder();
            String[] pinYinArray = pinYin.split(" ");
            for (int i = 0; i < pinYinArray.length; i++) {
                if (i == pinYinArray.length - 1) {
                    pinYinSx.append(pinYinArray[i].toLowerCase());
                } else {
                    pinYinSx.append(pinYinArray[i].substring(0, 1).toLowerCase());
                }
            }
            return pinYinSx.toString();
        }

        return pinYin;
    }
}
